package pkgBFPaint;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class DrawRubber extends Action {
	
	private Point from, to;
	private double m;
	
	public DrawRubber(Color color) {
		
		this.color = color;
		actionID = ActionID.DRAWRUBBER;
	}
	
	public DrawRubber() {
		
		actionID = ActionID.DRAWRUBBER;
	}
	
	public void setColor(Color color) {
		
		this.color = color;
	}
	
	@Override
	public void draw(Graphics g) {

		int i;

		g.setColor(color);

		if (points == null) {
			return;
		}

		from = points.getFirst();

		for (i = 1; i < points.size(); i++) {
			to = points.get(i);
			
			if (to.x != from.x) {	
				m = (double)(to.y - from.y) / (double)(to.x - from.x);
				
				if (Math.abs(m) < 1) {
					if (to.x > from.x) {
						for (int j = 0; j < to.x - from.x; j++)
							g.fillRect(from.x + j - 2, (int)(from.y + Math.round(j*m) - 2), 5, 5);
					}
					else {
						for (int j = 0; j < from.x - to.x; j++)
							g.fillRect(from.x - j - 2, (int)(from.y - Math.round(j*m) - 2), 5, 5);
					}	
				}
				else {
					if (to.y > from.y) {
						for (int j = 0; j < to.y - from.y; j++)
							g.fillRect((int)(from.x + Math.round(j/m) - 2), from.y + j - 2, 5, 5);
					}
					else {
						for (int j = 0; j < from.y - to.y; j++)
							g.fillRect((int)(from.x - Math.round(j/m) - 2), from.y - j - 2, 5, 5);
					}
				}
			}
			
			else {
				if (to.y > from.y)
					g.fillRect(from.x - 2, from.y - 2, 5, to.y - from.y);
				else
					g.fillRect(from.x - 2, to.y - 2, 5, from.y - to.y);
			}
		
			from = to;

		}
	}

}
